<?php
//财务记账
namespace App\Http\Controllers\Admin\Finance;

use App\Models\Finance\Monthstatis;
use App\Modules\Finance\Record;
use Illuminate\Http\Request;
use App\Http\Controllers\AdapterWebController;

class RecordController extends AdapterWebController
{
    /**
     * 财务记账中心
     */
    public function record(Request $request)
    {
        $postData = $request->all();
//        $this->appLayerExec(Record::class, 'record', $postData);
        return view('admin.finance.record.finance_center');
    }

    //固定成本投入
    public function cost(Request $request)
    {
        $res = $this->appLayerExec(Record::class, 'costList');
        return view('admin.finance.record.cost_record', ['data' => $res['res']]);
    }

    //添加成本
    public function addCost(Request $request)
    {
        if ($request->isMethod('get'))
            return view('admin.finance.record.add_cost');
        $postData = $request->except('_token');
        $res = $this->appLayerExec(Record::class, 'addCost', $postData);
        if ($res['status']) {
            flash('添加成功')->success();
            return redirect()->back();
        } else {
            flash('添加失败')->error();
        }
    }

    //新增sp月度报表
    public function addStatistics(Request $request)
    {
        if ($request->isMethod('post')) {
            $postData = $request->except('_token');
            $res = $this->appLayerExec(Record::class, 'addStatistics', $postData, false);
            if ($res['status'] == SUCCESS_STATUS) {
                flash($res['msg'])->success();
                return redirect()->back();
            } else {
                flash($res['msg'])->error();
            }
        }
        return view('admin.finance.record.add_statistics');
    }

    //成本统计
    public function costStatistics(Request $request)
    {
        $postData = $request->except('_token');
        $res = $this->appLayerExec(Record::class, 'costStatistics', $postData);
        return view('admin.finance.record.cost_statistics', ['data' => $res['res']['data'], 'search' => $res['res']['search'], 'selected' => $res['res']['selected']]);
    }


    //收入统计
    public function incomeStatistics(Request $request)
    {
        $res = $this->appLayerExec(Record::class, 'incomeStatistics');
        return view('admin.finance.record.income_statistics', ['list' => $res['res']['list'], 'total' => $res['res']['total']]);
    }

    //sp报表增加
    public function spIncome(Request $request)
    {
        $list = $this->appLayerExec(Record::class, 'spIncomeList');
        return view('admin.finance.record.sp_income', ['list' => $list['res']]);
    }

    //sp收入报表填写
    public function spInput(Request $request, $year, $month)
    {
        if ($request->isMethod('post')) {
            //执行excel上传
            $postData = $request->except('_token');
            $url = $this->fileUpload($request, 'excel');
            $postData['file_addr'] = $url['url'];
            $postData['original_name'] = $url['original_name'];
            $res = $this->appLayerExec(Record::class, 'spSubmit', $postData, false);

            if ($res['status'] != SUCCESS_STATUS) {
                $this->delFileByPath($url['url']);
                flash($res['msg'])->error();
                return redirect()->back();
            } else {
                flash($res['msg'])->success();
                return redirect()->back();
            }
        }
        $rqData = $request->except('_token');
        $rqData['year'] = $year;
        $rqData['month'] = $month;
        $temp = $this->appLayerExec(Record::class, 'spInputPrepare', $rqData, false);
        return view('admin.finance.record.sp_input', ['list' => $temp['res']['list'], 'search' => $temp['res']['search'], 'year' => $year, 'month' => $month]);
    }

    //根据id删除报表sp记录
    public function delInputById(Request $request)
    {
        $data = $request->only('id');
        $res = $this->appLayerExec(Record::class, 'delInputById', $data, false);
        if ($res['status'] != SUCCESS_STATUS) {
            flash($res['msg'])->error();
            return redirect()->back();
        } else {
            flash($res['msg'])->success();
            return redirect()->back();
        }
    }

    //分账sp管理
    public function spManagement(Request $request)
    {
        $list = $this->appLayerExec(Record::class, 'spList', null, false);
        return view('admin.finance.record.sp_management', ['list' => $list['res']]);
    }

    #成本统计(按月查询成本明细)
    public function costStatisticsCheck(Request $request)
    {
        $postData = $request->except('_token');
        $res = $this->appLayerExec(Record::class, 'costByMonth', $postData);
        return view('admin.finance.record.cost_statistics_check', ['data' => $res['res']]);
    }

    //增加sp
    public function financeAddSp(Request $request)
    {
        if ($request->isMethod('post')) {
            $postData = $request->except('_token');
            $res = $this->appLayerExec(Record::class, 'financeAddSp', $postData, false);
            if ($res['status'] != SUCCESS_STATUS) {
                flash($res['msg'])->error();
            }else{
                flash($res['msg'])->success();
            }
        }

        return view('admin.finance.record.add_sp');
    }

    //添加单条sp
    public function addOne(Request $request,$id)
    {
        if ($request->isMethod('post')) {
            $postData = $request->except('_token');
            $res = $this->appLayerExec(Record::class, 'addOne', $postData, false);
            if ($res['status'] != SUCCESS_STATUS) {
                flash($res['msg'])->error();
            }else{
                flash($res['msg'])->success();
            }
        }
        return view('admin.finance.record.add_one', ['id' => $id]);
    }

    //删除sp
    public function delSp($id)
    {
        $res = $this->appLayerExec(Record::class, 'delSp', ['id' => $id], false);
        if ($res['status'] != SUCCESS_STATUS) {
            flash($res['msg'])->error();
            return redirect('/admin/spManagement');
        }else{
            flash($res['msg'])->success();
            return redirect('/admin/spManagement');
        }
    }

    //删除单条sp类型
    public function delOne($id)
    {
        $res = $this->appLayerExec(Record::class, 'delSp', ['sonId' => $id], false);
        if ($res['status'] != SUCCESS_STATUS) {
            flash($res['msg'])->error();
            return redirect('/admin/spManagement');
        }else{
            flash($res['msg'])->success();
            return redirect('/admin/spManagement');
        }
    }

    //分账统计
    public function routingStatistics(Request $request)
    {
        if($request->input('time')){
            $time = $request->input('time');
        }else{
            $time = date('Y-m');
        }
        $res = Monthstatis::where('time','=',$time)->first();
        if(!empty($res)){
            $res = $res->toArray();
        }
        return view('admin.finance.record.routing_tatistics',['info'=>$res]);
    }
}
